﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConversionInterface;

namespace DataAccess
{
    public class MediaPersistence
    {
        private IPConverterDataSet.JOBDataTable jobs;
        private IPConverterDataSetTableAdapters.JOBTableAdapter jobAdapter = new DataAccess.IPConverterDataSetTableAdapters.JOBTableAdapter();
        
        public MediaPersistence()
        {
            this.jobAdapter.Connection.Open();
            this.jobs = this.jobAdapter.GetData();
            this.jobAdapter.Connection.Close();
        }
        
        public List<MediaFile> GetJobs()
        {
            List<MediaFile> files = new List<MediaFile>();
            foreach(IPConverterDataSet.JOBRow job in jobs)
            {
                MediaFile file = new MediaFile(job.INPUT_FILENAME);
                MediaFile.MediaLanguage chosenAudio, chosenSub;
                chosenAudio = (MediaFile.MediaLanguage)Enum.Parse(typeof(MediaFile.MediaLanguage), job.AUDIO_LANGUAGE);
                chosenSub = (MediaFile.MediaLanguage)Enum.Parse(typeof(MediaFile.MediaLanguage), job.SUB_LANGUAGE);

                foreach (AudioTrack track in file.AvailableAudio)
                    if (track.Language == chosenAudio)
                        file.ChosenAudio = track;
                foreach (SubtitleTrack track in file.AvailableSubs)
                    if (track.Language == chosenSub)
                        file.ChosenSub = track;

                file.TargetDevice = (ConvertMedia.ConversionTarget)Enum.Parse(typeof(ConvertMedia.ConversionTarget), job.TARGET_DEVICE);
                file.JobId = job.JOB_ID;
                file.OutputFilename = job.OUTPUT_FILENAME;

                if (job.FINISHED == true)
                    file.PercentComplete = 100;

                files.Add(file);
            }

            return files;
        }

        public void SaveJobs(List<MediaFile> files)
        {
            foreach (MediaFile file in files)
            {
                IPConverterDataSet.JOBRow row = null;
                if (file.JobId > 0)
                {
                    row = jobs.FindByJOB_ID(file.JobId);
                }
                else
                {
                    row = jobs.NewJOBRow();
                    row.DATE_CREATED = DateTime.Now;
                }

                row.SUB_LANGUAGE = file.ChosenSub.Language.ToString();
                row.AUDIO_LANGUAGE = file.ChosenAudio.Language.ToString();
                row.INPUT_FILENAME = file.InputFilename;
                row.OUTPUT_FILENAME = file.OutputFilename;
                row.TARGET_DEVICE = file.TargetDevice.ToString();
                row.FINISHED = file.PercentComplete == 100 ? true : false;

                if (row.RowState == System.Data.DataRowState.Detached)
                    jobs.AddJOBRow(row);
            }

            this.jobAdapter.Connection.Open();
            int count = this.jobAdapter.Update(jobs);
            this.jobAdapter.Connection.Close();
        }

        public void DeleteJob(MediaFile mediaFile)
        {
            if (mediaFile.JobId > 0)
            {
                jobAdapter.Delete(mediaFile.JobId);
            }
        }
    }
}
